package top.infopub.mgr.flow.web;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericController;
import top.infopub.mgr.flow.service.WsFlowProcessService;
import top.infopub.mgr.util.DateUtils;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;
import top.infopub.util.SpringContext;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;


/**
 * 所有工作流表单的控制层必须实现该类
 * @author Awoke
 * @version 2018年9月13日
 * @see GenericFlowController
 * @since
 */
public class GenericFlowController extends GenericController {

    protected final Logger log = LoggerFactory.getLogger(this.getClass());

    /**
     * 处理审批data
     * @param request
     * @param processId 
     * @see
     */
    protected void processData(HttpServletRequest request, String processId) {
        // 查询审批数据 
        Map<String, Object> map = SpringContext.getBean(WsFlowProcessService.class).queryProcAndAllNodes(
            processId);
        // 所有审批节点
        request.setAttribute("sharedAllNodes", map.get("sharedAllNodes"));
        // 当前节点
        request.setAttribute("sharedNowIdx", map.get("sharedNowIdx"));
        // 最大节点
        request.setAttribute("sharedMaxIdx", map.get("sharedMaxIdx"));
    }

    public ProjectAttachment createProjectFileAtta(AuthorizationVO vo, String proCode, String file) {
        //资料信息数组
        String[] fileAttStr = file.split("\\|");
        ProjectAttachment fileAtt = new ProjectAttachment();
        if(StringUtils.isNotEmpty(fileAttStr[2])){
            fileAtt.setId(fileAttStr[2]);
        }else{
            fileAtt.setId(IdFactory.getUUIDUpper());
        }
        ProjectFileTypeEnum pft = ProjectFileTypeEnum.getBySubCode(fileAttStr[0]);
        fileAtt.setTypes(pft.getCode());
        fileAtt.setSubTypes(pft.getSubCode());
//        fileAtt.setTypes(fileAttStr[0].substring(0, 1));//附件子分类：1=项目可研报告 2=两评一案 9=其他资料（选填）
        fileAtt.setProCode(proCode);//项目id
        //1|顺德锦荣-营业执照副本-202007|1f712de906d64893b6d5444998b7edad|/static/preProjectAtt/20210128/34d1c48297e2479abd0f563d823e0756.pdf
        //1|顺德锦荣-营业执照副本-202007.pdf|a626d86827ff4759bee96f27df0d1cc6|/static/preProjectAtt/20210128/ac970f7badd9493caf408e624ae4ce69.pdf
        String fileName = fileAttStr[1].substring(0, fileAttStr[1].lastIndexOf("."));
        String suffix = fileAttStr[1].substring(fileAttStr[1].lastIndexOf("."),
                fileAttStr[1].length()).toLowerCase();//后缀转小写
        fileAtt.setFileName(fileName);//文件名
        fileAtt.setFileSuffix(suffix);//文件后缀
        fileAtt.setFilePath(fileAttStr[3]);//文件保存路径
        fileAtt.setAddBy(vo.getId());//添加人
        fileAtt.setAddTime(DateUtils.getCurrentDateTime());//添加时间
        return fileAtt;
    }

    /**
     * 生成 对应 跳转到各业务审核页面的controller方法
     * @param bizTableName
     * @param bizSguid
     * @return
     * @see
     */
    public String bizControllerUrl(String bizTableName, String bizSguid, String proCode) {
        StringBuilder result = new StringBuilder("redirect:/");
        // 如把PRO_MASTER_INFO 转成 masterInfo
        String[] split = bizTableName.split("_");
        StringBuilder buffer = new StringBuilder();
        buffer.append(split[1].toLowerCase());
        for (int i = 2; i < split.length; i++ ) {
            String lowerCaseSplit = split[i].toLowerCase();
            String firstUpperChar = (lowerCaseSplit.charAt(0) + "").toUpperCase();
            buffer.append(firstUpperChar + lowerCaseSplit.substring(1));
        }
        result.append(buffer);
        // 【注意】：所有业务表单，跳转到审核页面必须统一命名：toDealOrDetail
        result.append("/toDealOrDetail?id=");
        result.append(bizSguid);
        result.append("&processDealWith=y&proCode=");
        result.append(proCode);
        return result.toString();
    }
}
